<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="审核状态" prop="reviewStatus">
        <el-select v-model="queryParams.reviewStatus" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.ho_application_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:changeApplication:edit']"
        >审核通过
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdateReturn"
          v-hasPermi="['system:changeApplication:edit']"
        >审核拒绝
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:changeApplication:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="changeApplicationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="人员ID" align="center" prop="studentId"/>
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="编号" align="center" prop="studentNum"/>
      <el-table-column label="申请时间" align="center" prop="applicationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applicationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请换宿时间" align="center" prop="requestedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.requestedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="换宿宿舍" align="center" prop="label" />
      <el-table-column label="申请信息" align="center" prop="applicationInfo"/>
      <el-table-column label="审核状态" align="center" prop="reviewStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ho_application_status" :value="scope.row.reviewStatus"/>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
  import {
    listChangeApplication,
    getChangeApplication,
    delChangeApplication,
    addChangeApplication,
    updateChangeApplication
  } from '@/api/system/changeApplication'

  export default {
    name: 'ChangeApplication',
    dicts: ['ho_application_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 换宿申请表格数据
        changeApplicationList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          studentId: 0,
          reviewStatus: '2',
          updateUserId: null
        },
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询换宿申请列表 */
      getList() {
        this.loading = true
        listChangeApplication(this.queryParams).then(response => {
          this.changeApplicationList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 审核通过按钮操作 */
      handleUpdate(row) {
        const id = row.id || this.ids[0]
        updateChangeApplication({
          id: id,
          reviewStatus: '1'
        }).then(response => {
          this.$modal.msgSuccess('审核通过成功')
          this.getList()
        })
      },
      /** 审核拒绝按钮操作 */
      handleUpdateReturn(row) {
        const id = row.id || this.ids[0]
        updateChangeApplication({
          id: id,
          reviewStatus: '0'
        }).then(response => {
          this.$modal.msgSuccess('审核拒绝成功')
          this.getList()
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/changeApplication/export', {
          ...this.queryParams
        }, `changeApplication_${new Date().getTime()}.xlsx`)
      }
    }
  }
</script>
